﻿/** 战国传承3,使用了比较特别的设计,用输入框指定连斩和空连的延时,
  并可以选择预设的几个延时.
  这些延时中金刚的是没怎么处理过的,因为这角色连击不好用我不用.
  连斩的效果并不很好,因为游戏有各种可能的拖慢情况,想玩好还是用单发斩.
  
  而重踢后的下下斩技能效果则意外的好,对多个角色有效,想玩得简单的话
  可以用.

  By setycyas @2023-10-14
*/

/* 全局设定
*/ {
#Warn
#MaxThreadsPerHotkey 1
SetWorkingDir(A_ScriptDir)
#Include "..\Class\FaceDetector.2.ahk"
#Include "..\Class\CommonMacro.2.ahk"
#Include "..\Class\CommonUI.2.ahk"
#Include "..\Class\MyFuns.2.ahk"
#Include "..\Class\MyEmu.2.ahk"
#Include "..\Class\OrderMap.2.ahk"
;SendMode("Event")
SendMode("Input")
}

/* 制作战国传承3的宏类
*/ class Sengoku3Macro extends CommonMacro {

  ;预设数据,在构造函数中再设定
  WEAPON_DURATIONS := Map()
  JUMP_DURATIONS := Map()
  ;角色表
  ROLES := ["红", "影连", "金鹰", "白鬼", "阿国", "金刚"]

  pressDuration := 30
  ;连斩间隔
  weaponDuration := []
  ;空连间隔,如第一个为0,则忽略并令第一击为拳,否则第一击为斩
  jumpDuration := []
  
  /* 构造函数,设定预设数据
  */
  __New(faceDetector, keys){
    super.__New(faceDetector, keys)
	  local wd := this.WEAPON_DURATIONS
    local jd := this.JUMP_DURATIONS
    wd["红"] := "370,400,310"
    wd["影连"] := "370,400,310"
    wd["金鹰"] := "370,400,310"
    wd["白鬼"] := "370,400,330"
    wd["阿国"] := "370,400,330"
    wd["金刚"] := "370,400,340"
    jd["红"] := "300,300,300"
    jd["影连"] := "300,300,300"
    jd["白鬼"] := "300,300,300"
    jd["金鹰"] := "250,300,250"
    jd["阿国"] := "0,250,300,250"
    jd["金刚"] := "350,350"
  }
  
  /* 新增的宏函数
  */
  _连发拳(key){
    ;兼容模拟单发,当autoFireDelay为0时转成单发并模拟按住的情况
	  local thisKey := MyFuns.getHotkey(key)
	  if (this.autoFireDelay > 0){
      loop {
        this._press(2)
      } until (MyFuns.waitKeyUp(thisKey, this.autoFireDelay))
	  } else {
	    this._down(2)
      while(MyFuns.getKeyState(thisKey)){
		    sleep(20)
	    }
	    this._up(2)
	  }
  }
  
  _连斩(key){
    ;合计4连
    local thisKey := MyFuns.getHotkey(key)
    local i, v, t
    this._press(1)
    for i, v in this.weaponDuration {
      if (MyFuns.waitKeyUp(thisKey, v, 50)) {
        break
      } else {
        this._press(1)
      }
    }
    ;msgbox(t)
  }
  
  _空连(key){
    local i, v
    ;第一击根据情况可能是斩或拳
    if (this.jumpDuration[1] != 0){
      this._press(1)
    } else {
      this._press(2)
    }
    ;后面的连击单数为拳(0被忽略不出招),双数为斩
    for i, v in this.jumpDuration {
      if (v == 0){
        continue
      }
      sleep(v)
      if (Mod(i, 2) != 0) {
        this._press(2)
      } else {
        this._press(1)
      }
    }
  }
  
  _下上AB(key){
    MyFuns.press("down", 50)
    sleep(50)
    MyFuns.press("up", 50)
    sleep(50)
    this._press(3)
    return
  }
  
  _重踢下下斩(key){
    this._press(2)
    sleep(150)
    this._下下攻击(this._keys[1])
  }
  
  /* 技能函数
  */
  ;key是攻击键
  _下下攻击(key){
    MyFuns.press("down")
    sleep(50)
    MyFuns.pressKeys(["down", key], [50, 50])
  }

  /* 重载宏接口
  */
  _setMacro(){
    this.macroMap := OrderMap()
    this.macroMap["连发拳"] := this._连发拳.bind(this)
    this.macroMap["连斩"] := this._连斩.bind(this)
    this.macroMap["空连"] := this._空连.bind(this)
    this.macroMap["下上AB"] := this._下上AB.bind(this)
    this.macroMap["重踢下下斩"] := this._重踢下下斩.bind(this)
    this.macroArr := this.macroMap.getKeys()
  }
}

/* 制作战国传承3专用UI
*/
class Sengoku3UI extends CommonUI {

  DEFAULT_HOTKEYS := "~d,~c,s,e,g"
  EXE_ARRAY := [MyEmu.WINKAWAKS, MyEmu.FBAS, MyEmu.XZONE]
  _weaponDurationEdit := "" ;连斩间隔设定
  _jumpDurationEdit := "" ;空连间隔设定

  /* 构造函数
  */
  __New(macro){
    super.__New("战国传承3", macro)
    this._preset := OrderMap(["默认", ["连发拳", "连斩", "空连", "下上AB", "重踢下下斩"]])
  }

  /* 重写接口
  */
  ;自定义gui组件标签页,增加一个骑马挑人延时
  _customGui(){
    super._customGui()
    this._myGui.Add("Text", "w100", "连斩间隔:")
    this._weaponDurationEdit := this._myGui.Add("Edit", "w100", "370,400,310")
    this._myGui.Add("Text", "w100", "空连间隔:")
    this._jumpDurationEdit := this._myGui.Add("Edit", "w100", "300,300,300")
    this._myGui.Add("Text", "w100", "选择预设数据:")
    this._roleComboBox := this._myGui.Add("ComboBox", "w100 choose1", 
      this._macro.ROLES)
    this._roleComboBox.OnEvent("Change", this._onRoleComboBox.bind(this))
  }
  
  ;提交数据后更新
  _onSubmitEnd(){
    super._onSubmitEnd()
    local i, v
    this._macro.weaponDuration := StrSplit(this._weaponDurationEdit.Text, ",")
    this._macro.jumpDuration := StrSplit(this._jumpDurationEdit.Text, ",")
  }
  
  ;响应预设数据选择
  _onRoleComboBox(obj, info){
    this._weaponDurationEdit.Text := 1
    this._weaponDurationEdit.Text := this._macro.WEAPON_DURATIONS[obj.Text]
    this._jumpDurationEdit.Text := this._macro.JUMP_DURATIONS[obj.Text]
  }

  /* 重写setup,更改可选的exe,读入预设
  */
  setup(){
    super.setup()
    this._myGui.opt("+AlwaysOnTop")
    this._myGui.show("x1100 y330")
    this.readPresetMenu("默认")
  }

}

/* 运行当前脚本
*/ if (A_ScriptName == "战国传承3.2.ahk") {
  face := FaceDetector() ;方向检测对象
  macro := Sengoku3Macro(face, ["q","f"]) ;宏对象,有界面时第二个参数可以随便写但长度不要小于2
  ;新建界面对象并显示
  ui := Sengoku3UI(macro)
  ui.setup()
  ;更新组件内容
  ui._exeComboBox.Choose(MyEmu.WINKAWAKS)
  ui._gameKeyEdit.Text := "q,w,p" ;最后一个是12组合键!
  ui._hotkeyEdit.Text := "~d,~c,s,e,g"
  ui._fpsEdit.Text := 10
  ui.readPresetMenu("默认")
}
